﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ClassBLL;
using ClassModel;
using ClassCommon;
using ClassWeb.App_Data;

namespace ClassWeb.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            // 绑定当前登录用户
            ViewBag.currentLoginer = this.CurrentUser;

            return View();
        }


        public ActionResult NewIndex()
        {
            ViewBag.currentLoginer = this.CurrentUser;

            return this.View();
        }

        /// <summary>
        /// 用户登录界面展示
        /// </summary>
        /// <returns></returns>
        [System.Web.Mvc.HttpGet]
        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(string userName, string password, string validateCode)
        {
            this.Response.ContentType = "application/json";

            // 服务端验证
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                return Json(new { status = false, info = "登录信息有误!" }, JsonRequestBehavior.AllowGet);

            int userID = t_classDemo_UserInfo_BLL.Login(userName, password);


            return Json(new { status = true, userID = userID, info = userID > 0 ? "登录成功!" : "用户名或者密码错误" }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Logout()
        {
            HttpCookieHelper.Logout();


            return Redirect("/Home");
        }


        /// <summary>
        /// 用户注册 GET 请求
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        public ActionResult Register()
        {
            return View();
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="validateCode">验证码</param>
        /// <returns></returns>
        [HttpPost()]
        public ActionResult Register(string userName, string password, string validateCode)
        {
            this.Response.ContentType = "application/json";

            // 服务器非空验证
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) return Redirect("/Template/static/Tip.html?tip=用户名和密码格式错误!");

            string codeFromCookie = HttpCookieHelper.getValidateCode();

            if (codeFromCookie.ToLower() != validateCode.ToLower()) return Redirect("/Template/static/Tip.html?tip=验证码错误!");

            t_classDemo_UserInfo userInfo = new t_classDemo_UserInfo();
            userInfo.userAccount = userName;
            userInfo.userPassword = password;
            userInfo.createTime = DateTime.Now;

            int userId = t_classDemo_UserInfo_BLL.InsertAccount(userInfo);

            string encryCookieUserInfo = EncryptHelper.Encrypt(userId.ToString(), string.Empty);

            HttpCookieHelper.SetCookie(ConfigSetting.Cookie_UserID, encryCookieUserInfo);

            return Json(new { status = userId > 0 }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 添加联系人信息
        /// </summary>
        /// <param name="location"></param>
        /// <param name="schoolname"></param>
        /// <param name="linkman"></param>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddContact(string location, string schoolname, string linkman, string phoneNum)
        {
            this.Response.ContentType = "application/json";

            //服务端验证
            t_class_contact contact = new t_class_contact();
            contact.location = location;
            contact.schoolName = schoolname;
            contact.linkMan = linkman;
            contact.phoneNum = phoneNum;
            contact.createtime = DateTime.Now;

            int increaseID = t_class_contact_BLL.InsertContact(contact);

            return Json(new { status = increaseID > 0 }, JsonRequestBehavior.AllowGet);
        }
    }
}
